package com.nf.health.app.intervention_details.fragment;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.customview.MyGridView;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.intervention_details.adapter.MeridianAdapter;
import com.nf.health.app.intervention_details.bean.Meridian;
import com.nf.health.app.intervention_details.bean.MeridianVo;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.ModelUtils;
import com.nf.health.app.utils.TextUtilTools;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 经络方案详情fragment
 * Created by Administrator on 2016/11/10.
 */
public class MeridiandetailsFragment extends IBaseFragment {

    MeridianVo mesridianSchemeVo = new MeridianVo();
    @Bind(R.id.tv_scheme_common_head_title)
    TextView tvSchemeCommonHeadTitle;//头
    @Bind(R.id.tv_scheme_common_head_content)//简介
            TextView tvSchemeCommonHeadContent;
    @Bind(R.id.yizhouanpai)//一周安排
            TextView yizhouanpai;
    @Bind(R.id.exercised_image)//经络图片
            ImageView exercisedImage;
    @Bind(R.id.exercised_blod_title)//经络名称
            TextView exercisedBlodTitle;
    @Bind(R.id.jianyi)
    TextView jianyi;
    @Bind(R.id.quxueweizhi)//取穴位置
            TextView quxueweizhi;
    @Bind(R.id.dongzuoyaoling)//动作要领
            TextView dongzuoyaoling;
    @Bind(R.id.gongxiao)//功效
            TextView gongxiao;
    @Bind(R.id.xueweipeiwu)//穴位配伍
            TextView xueweipeiwu;
    @Bind(R.id.zhuyi_listview)
    TextView zhuyiListview;//注意 textview
    @Bind(R.id.content_name)
    TextView contentname;//点击显示的经络名称 textview
    @Bind(R.id.meridian_gridview)
    MyGridView meridianGridview;//经络周期 gridview
    @Bind(R.id.quxue_title)
    LinearLayout quxueTitle;//取穴 title
    @Bind(R.id.dongzuo_title)
    LinearLayout dongzuoTitle;//动作 title
    @Bind(R.id.gongxiao_title)
    LinearLayout gongxiaoTitle;//功效 title
    @Bind(R.id.xuewei_title)
    LinearLayout xueweiTitle;//穴位 title
    @Bind(R.id.zhuyi_title)
    LinearLayout zhuyiTitle;//注意 title
    @Bind(R.id.exerc_lin)
    LinearLayout exercLin;
    @Bind(R.id.title_lin)
    LinearLayout titleLin;
    @Bind(R.id.yizhou_lin)
    LinearLayout yizhouLin;
    @Bind(R.id.title_image_lin)
    LinearLayout titleImageLin;


    private int selectdataTypePosition = 0;//默认选中第一条

    MeridianAdapter meridianAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_meridiandetails);
        ButterKnife.bind(this, rootView);
        initView();
    }

    private void initView() {
        String id = Constants.CHANGE_ABLE.INTERVENTION_ID;
        provider.requesmeridian(id, "exercised");
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if ("exercised".equals(actionName)) {
            mesridianSchemeVo = (MeridianVo) object;

            tvSchemeCommonHeadTitle.setText("经络方案");
            tvSchemeCommonHeadContent.setText(mesridianSchemeVo.getGuide());

            yizhouanpai.setText("一周监测安排表" + "(" + DateUtil.dateToString(new Date(Long.parseLong(mesridianSchemeVo.getStarttime())), "yyyy-MM-dd") + "至" + DateUtil.dateToString(new Date(Long.parseLong(mesridianSchemeVo.getEndtime())), "yyyy-MM-dd") + ")");

            if (TextUtils.isEmpty(mesridianSchemeVo.getSuggest())) {
                exercLin.setVisibility(View.GONE);
            } else {
                exercLin.setVisibility(View.VISIBLE);
                jianyi.setText(mesridianSchemeVo.getSuggest());
            }
            //随点击变化
            if (mesridianSchemeVo.getList().size() > 0) {
                ImageLoaderUtils.getinstance(getActivity()).getImage(exercisedImage, mesridianSchemeVo.getList().get(0).getPicture(), R.drawable.default_image);
                exercisedBlodTitle.setText("-" + mesridianSchemeVo.getList().get(0).getName() + "-");
                contentname.setText(mesridianSchemeVo.getList().get(0).getName());
//                quxueweizhi.setText(mesridianSchemeVo.getList().get(0).getPosition());//取穴位置
//                dongzuoyaoling.setText(mesridianSchemeVo.getList().get(0).getAct());//动作要领
//                gongxiao.setText(mesridianSchemeVo.getList().get(0).getEffect());//功效作用
//                xueweipeiwu.setText(mesridianSchemeVo.getList().get(0).getAssigned());//穴位配伍
                String StrS = mesridianSchemeVo.getList().get(0).getAttention();

                meridianAdapter = new MeridianAdapter(getActivity(), mesridianSchemeVo.getList());
                meridianGridview.setAdapter(meridianAdapter);
                mesridianSchemeVo.getList().get(0).setIsClick(true);
                meridianGridview.setSelection(0);

                //取穴位置
                String StrS1 = mesridianSchemeVo.getList().get(0).getPosition();
                if (StrS1.contains("||")) {
                    String result[] = StrS1.split("\\|\\|");
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < result.length; i++) {
                        sb.append(result[i] + "\n");
                    }
                   // quxueweizhi.setText(TextUtils.isEmpty(sb) ? "" : Html.fromHtml(sb.toString()));
                    //quxueweizhi.setMovementMethod(LinkMovementMethod.getInstance());
                    if(TextUtils.isEmpty(sb.toString())){
                        quxueweizhi.setText("");
                    }else {
                        quxueweizhi.setText(sb.toString());
                    }
                } else {
                    if (TextUtils.isEmpty(StrS1)) {
                        quxueTitle.setVisibility(View.GONE);
                    } else {
                        quxueTitle.setVisibility(View.VISIBLE);
                        quxueweizhi.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(0).getPosition()) ? "" : mesridianSchemeVo.getList().get(0).getPosition());
                    }

                }
                //动作要领
                String StrS2 = mesridianSchemeVo.getList().get(0).getAct();
                if (StrS2.contains("||")) {
                    String result[] = StrS2.split("\\|\\|");
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < result.length; i++) {
                        sb.append(result[i] + "\n");
                    }

                    if(TextUtils.isEmpty(sb.toString())){
                        dongzuoyaoling.setText("");
                    }else {
                        dongzuoyaoling.setText(sb.toString());
                    }
                } else {
                    if (TextUtils.isEmpty(StrS2)) {
                        dongzuoTitle.setVisibility(View.GONE);
                    } else {
                        dongzuoTitle.setVisibility(View.VISIBLE);
                        dongzuoyaoling.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(0).getAct()) ? "" : mesridianSchemeVo.getList().get(0).getAct());
                    }
                }
                //功效作用
                String StrS3 = mesridianSchemeVo.getList().get(0).getEffect();
                if (StrS3.contains("||")) {
                    String result[] = StrS3.split("\\|\\|");
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < result.length; i++) {
                        //sb.append("<font color='#2E86F4'>" + (i + 1) + "." + "</font>" + result[i] + "<br>");
                        sb.append(result[i] + "\n");
                    }
                    if(TextUtils.isEmpty(sb.toString())){
                        gongxiao.setText("");
                    }else {
                        gongxiao.setText(sb.toString());
                    }
                } else {
                    if (TextUtils.isEmpty(StrS3)) {
                        gongxiaoTitle.setVisibility(View.GONE);
                    } else {
                        gongxiaoTitle.setVisibility(View.VISIBLE);
                        gongxiao.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(0).getEffect()) ? "" : mesridianSchemeVo.getList().get(0).getEffect());
                    }
                }
                //穴位配伍

                String StrS4 = mesridianSchemeVo.getList().get(0).getAssigned();
                if (StrS4.contains("||")) {
                    String result[] = StrS4.split("\\|\\|");
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < result.length; i++) {
                        //sb.append("<font color='#2E86F4'>" + (i + 1) + "." + "</font>" + result[i] + "<br>");
                        sb.append(result[i] + "\n");
                    }
                    if(TextUtils.isEmpty(sb.toString())){
                        xueweipeiwu.setText("");
                    }else {
                        xueweipeiwu.setText(sb.toString());
                    }
                } else {
                    if (TextUtils.isEmpty(StrS4)) {
                        xueweiTitle.setVisibility(View.GONE);
                    } else {
                        xueweiTitle.setVisibility(View.VISIBLE);
                        xueweipeiwu.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(0).getAssigned()) ? "" : mesridianSchemeVo.getList().get(0).getAssigned());
                    }
                }


                if (StrS.contains("||")) {
                    String result[] = StrS.split("\\|\\|");
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < result.length; i++) {
                        //sb.append("<font color='#2E86F4'>" + (i + 1) + "." + "</font>" + result[i] + "<br>");
                        sb.append(result[i] + "\n");
                    }
                    if(TextUtils.isEmpty(sb.toString())){
                        zhuyiListview.setText("");
                    }else {
                        zhuyiListview.setText(sb.toString());
                    }
                } else {
                    if (TextUtils.isEmpty(StrS)) {
                        zhuyiTitle.setVisibility(View.GONE);
                    } else {
                        zhuyiTitle.setVisibility(View.VISIBLE);
                        zhuyiListview.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(0).getAttention()) ? "" : mesridianSchemeVo.getList().get(0).getAttention());
                    }
                }
            } else {
                //数据为空  全部隐藏
                exercLin.setVisibility(View.GONE);
                dongzuoTitle.setVisibility(View.GONE);
                gongxiaoTitle.setVisibility(View.GONE);
                xueweiTitle.setVisibility(View.GONE);
                zhuyiTitle.setVisibility(View.GONE);
                quxueTitle.setVisibility(View.GONE);
                titleLin.setVisibility(View.GONE);
                yizhouLin.setVisibility(View.GONE);
                titleImageLin.setVisibility(View.GONE);
            }

            meridianGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Meridian mySelectpayTime = mesridianSchemeVo.getList().get(position);
                    selectdataTypePosition = position;
                    mySelectpayTime.setIsClick(true);
                    ModelUtils.dealNotSelectStatus3(mesridianSchemeVo.getList(), position, meridianAdapter);


                    ImageLoaderUtils.getinstance(getActivity()).getImage(exercisedImage, mesridianSchemeVo.getList().get(position).getPicture(), R.drawable.default_image);
                    exercisedBlodTitle.setText("-" + mesridianSchemeVo.getList().get(position).getName() + "-");
                    contentname.setText(mesridianSchemeVo.getList().get(position).getName());

//                    quxueweizhi.setText(mesridianSchemeVo.getList().get(position).getPosition());
//                    dongzuoyaoling.setText(mesridianSchemeVo.getList().get(position).getAct());
//                    gongxiao.setText(mesridianSchemeVo.getList().get(position).getEffect());
//                    xueweipeiwu.setText(mesridianSchemeVo.getList().get(position).getAssigned());
                    //取穴位置
                    String StrS1 = mesridianSchemeVo.getList().get(position).getPosition();
                    if (StrS1.contains("||")) {
                        String result[] = StrS1.split("\\|\\|");
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < result.length; i++) {
                            sb.append(result[i] + "\n");
                        }
                        quxueweizhi.setText(TextUtils.isEmpty(sb) ? "" : sb.toString());
                    } else {
                        if (TextUtils.isEmpty(StrS1)) {
                            quxueTitle.setVisibility(View.GONE);
                        } else {
                            quxueTitle.setVisibility(View.VISIBLE);
                            quxueweizhi.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(position).getPosition()) ? "" : mesridianSchemeVo.getList().get(position).getPosition());
                        }
                    }
                    //动作要领
                    String StrS2 = mesridianSchemeVo.getList().get(position).getAct();
                    if (StrS2.contains("||")) {
                        String result[] = StrS2.split("\\|\\|");
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < result.length; i++) {
                            sb.append(result[i] + "\n");
                        }
                        dongzuoyaoling.setText(TextUtils.isEmpty(sb) ? "" : sb.toString());
                    } else {
                        if (TextUtils.isEmpty(StrS2)) {
                            dongzuoTitle.setVisibility(View.GONE);
                        } else {
                            dongzuoTitle.setVisibility(View.VISIBLE);
                            dongzuoyaoling.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(position).getAct()) ? "" : mesridianSchemeVo.getList().get(position).getAct());
                        }
                    }
                    //功效作用
                    String StrS3 = mesridianSchemeVo.getList().get(position).getEffect();
                    if (StrS3.contains("||")) {
                        String result[] = StrS3.split("\\|\\|");
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < result.length; i++) {
                            sb.append(result[i] + "\n");
                        }
                        gongxiao.setText(TextUtils.isEmpty(sb) ? "" : sb.toString());
                    } else {
                        if (TextUtils.isEmpty(StrS3)) {
                            gongxiaoTitle.setVisibility(View.GONE);
                        } else {
                            gongxiaoTitle.setVisibility(View.VISIBLE);
                            gongxiao.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(position).getEffect()) ? "" : mesridianSchemeVo.getList().get(position).getEffect());
                        }
                    }
                    //穴位配伍

                    String StrS4 = mesridianSchemeVo.getList().get(position).getAssigned();
                    if (StrS4.contains("||")) {
                        String result[] = StrS4.split("\\|\\|");
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < result.length; i++) {
                            sb.append(result[i] + "\n");
                        }
                        xueweipeiwu.setText(TextUtils.isEmpty(sb) ? "" : sb.toString());
                    } else {

                        if (TextUtils.isEmpty(StrS4)) {
                            xueweiTitle.setVisibility(View.GONE);
                        } else {
                            xueweiTitle.setVisibility(View.VISIBLE);
                            xueweipeiwu.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(position).getAssigned()) ? "" : mesridianSchemeVo.getList().get(position).getAssigned());
                        }
                    }
                    String StrS = mesridianSchemeVo.getList().get(position).getAttention();
                    if (StrS.contains("||")) {
                        String result[] = StrS.split("\\|\\|");
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < result.length; i++) {
                            sb.append(result[i] + "\n");
                        }
                        zhuyiListview.setText(TextUtils.isEmpty(sb) ? "" : sb.toString());
                    } else {
                        if (TextUtils.isEmpty(StrS)) {
                            zhuyiTitle.setVisibility(View.GONE);
                        } else {
                            zhuyiTitle.setVisibility(View.VISIBLE);
                            zhuyiListview.setText(TextUtils.isEmpty(mesridianSchemeVo.getList().get(position).getAttention()) ? "" : mesridianSchemeVo.getList().get(position).getAttention());
                        }
                    }
                }
            });


        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}